package com.rabbiter.oes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.entity.Message;
import com.rabbiter.oes.result.R;
import com.rabbiter.oes.service.impl.MessageServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "留言")
@RestController
public class MessageController {

    @Autowired
    private MessageServiceImpl messageService;

    @ApiOperation(value = "查询所有留言信息", notes = "查询所有留言信息", httpMethod = "GET")
    @GetMapping("/messages/{page}/{size}")
    public R<IPage<Message>> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<Message> messagePage = new Page<>(page,size);
        IPage<Message> all = messageService.findAll(messagePage);
        return R.<IPage<Message>>ok("查询所有留言").data(all);
    }

    @ApiOperation(value = "根据id查询留言信息", notes = "更具id查询留言信息", httpMethod = "GET")
    @GetMapping("/message/{id}")
    public R findById(@PathVariable("id") Integer id) {
        Message res = messageService.findById(id);
        return R.ok("根据Id查询").data(res);
    }

    @ApiOperation(value = "根据id删除留言信息", notes = "根据id删除留言信息", httpMethod = "DELETE")
    @DeleteMapping("/message/{id}")
    public R delete(@PathVariable("id") Integer id) {
        int res = messageService.delete(id);
        if (res > 0) {
            return R.ok("删除成功！");
        }else {
            return R.logicError("删除失败！");
        }
    }

    @ApiOperation(value = "添加留言信息", notes = "添加留言信息", httpMethod = "POST")
    @PostMapping("/message")
    public R add(@RequestBody Message message) {
        int res = messageService.add(message);
        if (res == 0) {
            return R.logicError("添加失败").data(res);
        } else {
            return R.ok("添加成功").data(res);
        }
    }
}
